package androidtopc;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import javax.imageio.ImageIO;

public class DebugTemp {
	private final String HOME_DIR = "C:/dev/UROP/images/";
	private final String IMAGE_TYPE = "jpg";
	private final String IMAGE_EXTENSION = "." + IMAGE_TYPE;

	private final int BUFFER_SIZE = (int) Math.pow(2, 15); // Max buffer for
															// android

	byte[] imageToByte(final String filePath, final String imageType) {
		try {
			// Read Image
			BufferedImage bimg = ImageIO.read(new File(filePath));

			// Convert and add to byte stream
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(bimg, imageType, baos);
			baos.flush();

			// Store in byte array
			byte[] imageInByte = baos.toByteArray();
			baos.close();

			return imageInByte;
		} catch (IOException e) {
			System.out.println(e.getMessage());
			return null;
		}
	}

	private BufferedImage byteToBufferedImage(byte[] byteImage, String imageType) {
		try {
			InputStream input = new ByteArrayInputStream(byteImage);
			BufferedImage bimg = ImageIO.read(input);
			return bimg;
		} catch (IOException e) {
			System.err
					.println("Could not converted byte image to buffered image");
			return null;
		}
	}

	void saveImage(BufferedImage image, String filePath) {
		try {
			System.out.println("Saving image");
			File outputfile = new File(filePath);
			ImageIO.write(image, IMAGE_TYPE, outputfile);
		} catch (IOException e) {
			System.err.print("Could not save image file.");
		}
	}

	void sendImage(String filePath, String imageType) {
		System.out.println("Sending image");

		// Convert buffered image to byte array
		byte[] byteImage = imageToByte(filePath, imageType);
		// Send size of the incoming image
		System.out.println("Image Size: " + byteImage.length);

		// Get number of streams that are equal to buffer size
		int numBuffers = byteImage.length / BUFFER_SIZE;
		// Stream each small stream
		byte[] debug = new byte[byteImage.length];
		for (int i = 0; i < numBuffers * BUFFER_SIZE; i += BUFFER_SIZE) {
			System.out.println("writing stream");
			byte[] buffer = Arrays.copyOfRange(byteImage, i, i + BUFFER_SIZE);

			System.arraycopy(buffer, 0, debug, i, buffer.length);
		}
		
		for(int i = 0; i< numBuffers * BUFFER_SIZE; i ++){
			if(byteImage[i] != debug[i]){
				System.out.println("Bad Index: " +i);
				break;
			}
		}
		
		// Send the end of the image array
		byte[] remainingBuffer = Arrays.copyOfRange(byteImage, numBuffers
				* BUFFER_SIZE, byteImage.length);
		System.arraycopy(remainingBuffer, 0, debug, debug.length-remainingBuffer.length, remainingBuffer.length);
		
		BufferedImage bimg = byteToBufferedImage(debug, IMAGE_TYPE);
		saveImage(bimg, "C:/dev/UROP/images/test2.jpg");
	}
	
	public static void main(String args[]) {
		DebugTemp client = new DebugTemp();
		client.sendImage("C:/dev/UROP/images/test.jpg", "jpg");
	}
}
